package crivia.eca.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import crivia.eca.carry.CCKV;
import crivia.ecp.common.ECR;

public class CatConfigureReader {
	
	public static String wash(String t){
		if (t.indexOf("#") < 0)
			return t.trim();
		String s = t.trim();
		s = s.substring(0 , s.indexOf("#"));
		return s.startsWith(",") ? s.substring(1).trim() : s.trim();
	}
	
	public static String readWorkFlowID(String configureText){
		String[] ss = configureText.split("\n");
		for (String t : ss){
			if (t.trim().startsWith("#")){
				continue;
			}
			String s = wash(t);
			if ("".equals(s)){
				continue;
			}
			if (s.indexOf(":") > -1){
				continue;
			}
			if (ECR.toNumber(s, int.class) > 0){
				return s;
			}
		}
		return null;
	}
	
	public static String readNodeID_String(String configureText , int index){
		String[] ss = configureText.split("\n");
		int n = 0;
		for (String t : ss){
			if (t.trim().startsWith("#")){
				continue;
			}
			String s = wash(t);
			if ("".equals(s)){
				continue;
			}
			if (s.indexOf(":") > -1){
				continue;
			}
			if (ECR.toNumber(s, int.class) > 0){
				if (n < index){
					n++;
				} else {
					return s;
				}
			}
		}
		return null;
	}
	
	public static String readNodeID_String(String configureText , String key){
		String[] ss = configureText.split("\n");
		for (String t : ss){
			if (t.trim().startsWith("#")){
				continue;
			}
			String s = wash(t);
			if ("".equals(s)){
				continue;
			}
			if (s.indexOf(":") < 0){
				continue;
			}
			String[] vk = s.split(":");
			if (ECR.toNumber(vk[0].trim(), int.class) > 0){
				if (vk[1].trim().toLowerCase().equals(key.toLowerCase())){
					return vk[0].trim();
				}
			}
		}
		return null;
	}
	
	public static List<String> readIDs(String configureText){
		String[] ss = configureText.split("\n");
		List<String> l = new ArrayList<String>();
		for (String t : ss){
			if (t.trim().startsWith("#")){
				continue;
			}
			String s = wash(t);
			if ("".equals(s)){
				continue;
			}
			if (s.indexOf(":") > -1){
				continue;
			}
			if (ECR.toNumber(s, int.class) > 0){
				l.add(s);
			}
		}
		return l;
	}
	
	public static int readNodeID_Number(String configureText , int index){
		String[] ss = configureText.split("\n");
		int n = 0;
		for (String t : ss){
			if (t.trim().startsWith("#")){
				continue;
			}
			String s = wash(t);
			if ("".equals(s)){
				continue;
			}
			if (s.indexOf(":") > -1){
				continue;
			}
			if (ECR.toNumber(s, int.class) > 0){
				if (n < index){
					n++;
				} else {
					return ECR.toNumber(s, int.class);
				}
			}
		}
		return 0;
	}
	
	public static int readNodeID_Number(String configureText , String key){
		String[] ss = configureText.split("\n");
		for (String t : ss){
			if (t.trim().startsWith("#")){
				continue;
			}
			String s = wash(t);
			if ("".equals(s)){
				continue;
			}
			if (s.indexOf(":") < 0){
				continue;
			}
			String[] vk = s.split(":");
			if (ECR.toNumber(vk[0].trim(), int.class) > 0){
				if (vk[1].trim().toLowerCase().equals(key.toLowerCase())){
					return ECR.toNumber(vk[0].trim(), int.class);
				}
			}
		}
		return 0;
	}
	
	public static Map<String , String> readIDMap(String configureText){
		String[] ss = configureText.split("\n");
		Map<String , String> m = new HashMap<String, String>();
		for (String t : ss){
			if (t.trim().startsWith("#")){
				continue;
			}
			String s = wash(t);
			if ("".equals(s)){
				continue;
			}
			if (s.indexOf(":") < 0){
				continue;
			}
			String[] vk = s.split(":");
			if (ECR.toNumber(vk[0].trim(), int.class) > 0){
				m.put(vk[1].trim(), vk[0].trim());
			}
		}
		return m;
	}
	
	public static String readField(String configureText , int index){
		String[] ss = configureText.split("\n");
		int n = 0;
		for (String t : ss){
			if (t.trim().startsWith("#")){
				continue;
			}
			String s = wash(t);
			if ("".equals(s)){
				continue;
			}
			if (s.indexOf(":") > -1){
				continue;
			}
			if (ECR.toNumber(s, int.class) == 0){
				if (n < index){
					n++;
				} else {
					return s;
				}
			}
		}
		return null;
	}
	
	public static String readField(String configureText , String key){
		if (null == key){
			return null;
		}
		String[] ss = configureText.split("\n");
		for (String t : ss){
			if (t.trim().startsWith("#")){
				continue;
			}
			String s = wash(t);
			if ("".equals(s)){
				continue;
			}
			if (s.indexOf(":") < 0){
				continue;
			}
			String[] vk = s.split(":");
			if (ECR.toNumber(vk[0].trim(), int.class) == 0){
				if (vk[1].trim().toLowerCase().equals(key.toLowerCase())){
					return vk[0].trim();
				}
			}
		}
		return null;
	}
	
	public static List<String> readFields(String configureText){
		String[] ss = configureText.split("\n");
		List<String> l = new ArrayList<String>();
		for (String t : ss){
			if (t.trim().startsWith("#")){
				continue;
			}
			String s = wash(t);
			if ("".equals(s)){
				continue;
			}
			if (s.indexOf(":") > -1){
				continue;
			}
			if (ECR.toNumber(s, int.class) == 0){
				l.add(s);
			}
		}
		return l;
	}
	
	public static Map<String , String> readFieldMap(String configureText){
		String[] ss = configureText.split("\n");
		Map<String , String> m = new HashMap<String, String>();
		for (String t : ss){
			if (t.trim().startsWith("#")){
				continue;
			}
			String s = wash(t);
			if ("".equals(s)){
				continue;
			}
			if (s.indexOf(":") < 0){
				continue;
			}
			String[] vk = s.split(":");
			if (ECR.toNumber(vk[0].trim(), int.class) == 0){
				m.put(vk[1].trim(), vk[0].trim());
			}
		}
		return m;
	}
	
	public static String setIndex(String configureText){
		String[] configureRows = configureText
				.replaceAll("#Field \\[[^\\]]+]", "#")
				.replaceAll("#Node \\[[^\\]]+]", "#")
				.replaceAll("#Workflow \\[[^\\]]+]", "#")
				.split("\n");
//		System.out.println(configureText
//				.replaceAll("#Field \\[[^\\]]+]", "#")
//				.replaceAll("#Node \\[[^\\]]+]", "#")
//				.replaceAll("#Workflow \\[[^\\]]+]", "#")
//				);
		int ni = 0;
		int fi = 0;
		StringBuilder text = new StringBuilder();
		for (String row : configureRows){
			if (row.trim().startsWith("#")){
				text.append(row).append("\n");
				continue;
			}
			String s = wash(row);
			if ("".equals(s)){
				text.append(row).append("\n");
				continue;
			}
			if (s.indexOf(":") > -1){
				text.append(row).append("\n");
				continue;
			}
			if (ECR.toNumber(s, int.class) > 0){
				if (0 == ni){
					text.append(" ").append(s).append(" #Workflow [").append(ni++).append("] ");
				} else {
					text.append(" ").append(s).append(" #Node [").append(ni++).append("] ");
				}
			} else {
				text.append(" ").append(s).append(" #Field [").append(fi++).append("] ");
			}
			if (row.indexOf("#") > -1){
				text.append(row.substring(row.indexOf("#") + 1));
			}
			text.append("\n");
		}
		return text.toString();
	}
	
	public static List<CCKV> allCCKVs(String configureText){
		List<CCKV> kvs = new ArrayList<CCKV>();
		String[] ss = configureText.split("\n");
		for (String t : ss){
			if (t.trim().startsWith("#")){
				continue;
			}
			String s = wash(t);
			if ("".equals(s)){
				continue;
			}
			if (s.indexOf(":") < 0){
				continue;
			}
			String[] vk = s.split(":");
			CCKV kv = new CCKV();
			kvs.add(kv);
			kv.setKey(vk[1].trim());
			kv.setValue(vk[0].trim());
		}
		return kvs;
	}

}
